<?php
 class orden extends Controller{
 	    function __construct(){
 	    	parent::Controller();
 	    	$this->load->model('morden');
 	    	$this->load->model('mnumeroorden');
 	    	$this->load->model('mprovincia2');
 	    	$this->load->model('mtransaccionentrada');
 	    	$this->load->model('detallerentradas');
                    $this->load->model('mservicios');
 	    }
 	    
 	    function index(){
 	    	$this->load->view('main', array('content_page'=>'orden_list','menutop' => 0));
 	    }
 	    
 	    function historico($id){
 	    	 
 	    	 $array = array('id_statu'      =>1,
 	    	 				'IdUsuarioOrden'=>$this->session->userdata('LOGGED_USR_ID'));
 	    	
 	    	  if($this->morden->update($array,$id) == true){
 	    	  	
 	    	  	    redirect('orden');
 	    	  	    
 	    	   	}
 	    }
 	    
 	    function inbaund($id){
 	    	
 	    	 $orden = $this->morden->LoadCompaniaById($id);
 	    	 $date = date('Y-m-d');
 	    	 
 	    	 if ($orden->getTipoServicio() == 1) {
 	    	 	$servicio = 2;
 	    	 }else{
 	    	 	$servicio = 1;
 	    	 } 
 	    	 
 	    	 $data = array('DateSolicitud'     => $date,
 	    	               'DateSistema'       => $date,
 	    	               'idTipoEntrada'     => 7,
 	    	               'idServicios'       => $servicio,
 	    	               'referencia'        => $orden->getDescriccion(),
 	    	               'telefono1'         => $orden->getTelefono(),
 	    	               'telefono2'         => $orden->getTelefono2(),
 	    	               'email'             => $orden->getMail(),
 	    	               'ProximoContacto'   => $date,
 	    	               'IdUsuario'         => $this->session->userdata('LOGGED_USR_ID'),
 	    	               'Contacto'          => $orden->getContacto(),
 	    	 			   'Campania'          => $orden->getIdCampania()); 
 	    	 			          
 	    	
 	    	$this->mtransaccionentrada->createRegistro($data);
 	    	  
 	    	$idtransacion = $this->mtransaccionentrada->LoadjurisdiccionByName($orden->getDescriccion(),$orden->getMail());
 	    	$arraydata = array('ContenidoDetalle'    => 'Trasladado de orden a inbaund',
 	    	                   'IdTrasacionEntrada'  => $idtransacion->getIdTransaccion(),
 	    	                   'IdUsuario'           => $this->session->userdata('LOGGED_USR_ID'),
 	    	                   'DateSistema'         => date("Y-m-d H:i:s"),
 	    					   'TimeSistema'         => date("H:i:s"),
 	    					   'idTipoSeguimiento'   => 2);
 	    	
 	    	$this->detallerentradas->createDetalle($arraydata);
 	    	
 	    	$array = array('id_statu'=>1);
 	    	
 	    	  if ( $this->morden->update($array,$id) == true) {
 	    	  	    redirect('orden');
 	    	  }
 	    }
 	    
 	    function data(){
 	        $page = $this->input->post('page') ? $this->input->post('page') : 1; // Pagina Actual o 1 por default
			$sortname = $this->input->post('sidx') ? $this->input->post('sidx') : 'ID_airport'; // Columna para el order by
			$sortorder = $this->input->post('sord') ? $this->input->post('sord') : 'asc'; // Orden (asc|desc)
			$limit = $this->input->post('rows') ? $this->input->post('rows') : 10;
		   
			header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
			header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
			header("Cache-Control: no-cache, must-revalidate" );
			header("Pragma: no-cache" );
			
			$records = $this->morden->loadOrden();

			$totalRecords = $this->morden->GetCount();

			$totalPages = ceil($totalRecords/$limit);
			if ($page > $totalPages) $page = $totalPages;
			
			$json_output = array(
				'page'		=> $page,
				'total'		=> $totalPages,
				'records'	=> $totalRecords,
				'rows'		=> array()
			);
			
                              $servicio = "";
                        
				if($records != false) {
				foreach($records as $estatus) {
					  
					   
				$servicio = $this->mservicios->LoadServicioById($estatus->getTipoServicio());	   
			 $norden = $this->mnumeroorden->LoadCompaniaById($estatus->getNumeroOrden());		 
			 $provincia = $this->mprovincia2->LoadCompaniaById($estatus->getProvincia());	   
					
					$arr = $json_output['rows'][] = array(
					                                      'id_orden' => $estatus->getIdOrden(),
					                                      'descricion' => $estatus->getDescriccion(), 
					                                      'tipo_servicio' => $servicio->getNombreServicio(),
					                                      'provincia' => $provincia->getDescriccion(),
					                                      'name_contacto' => $estatus->getContacto(),
					                                      'telefono' => $estatus->getTelefono(),
					                                      'date' => $estatus->getDate(),
					                                      'mail' => $estatus->getMail(), 
					                                      'idnum' => $norden->getDescriccion(),
					                                      'IdCampania' =>$estatus->getIdCampania());
				}
			}
			
		 echo json_encode($json_output);
		 	
 	    }
    	
 	
 }

